package jaux.tank.generator.component;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import jaux.tank.generator.domain.TankConstant;
import jaux.tank.generator.jd.components.JdComboBox;
import jaux.tank.generator.jd.components.JdTextField;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ConstantEditorPanel extends JPanel{

	private static final long serialVersionUID = 1L;
	@Getter
	private TankConstant constant;
	
	@Getter
	@Setter
	private boolean saved;
	
	public ConstantEditorPanel(TankConstant constant) {
		super();
		if(constant ==null) {
			this.constant = new TankConstant("");
		}else {
			this.constant = constant;
		}
		initializeComponents();
	}
	
	private void initializeComponents() {
		setLayout(new FlowLayout(FlowLayout.LEFT, 10,5));
		JdTextField nameTextField = new JdTextField("Name:", constant.getName());
		nameTextField.setEditable(false);
		add(nameTextField );
		// type field
		JdComboBox<TankConstant.ConstantType> typeComboBox = new JdComboBox<>("Type:",constant.getType(), TankConstant.ConstantType.values());
		typeComboBox.onChange(value -> {
			constant.setType(value);
			this.saved = false;
		});
		add(typeComboBox );
		// value field
		JdTextField valueTextField = new JdTextField("Value:", constant.getValue());
		valueTextField.onChange(value -> {
			constant.setValue(value);
			this.saved = false;
		});
		add(valueTextField );
	}
	
	public static void main(String[] args) {
		// test
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.setLayout(new BorderLayout());
		ConstantEditorPanel constantEditorPanel = new ConstantEditorPanel(null);
		frame.add(constantEditorPanel, BorderLayout.CENTER);
		JButton getValueBtn = new JButton("get value");
		frame.add(getValueBtn, BorderLayout.SOUTH);
		getValueBtn.addActionListener(e->{
			JOptionPane.showMessageDialog(frame,"changed:"+!constantEditorPanel.isSaved()+":"+ constantEditorPanel.getConstant());
		});
		frame.pack();
		frame.setVisible(true);
	}

}
